<?php
namespace Admin\Controller;
class UserController extends BaseController {
    var $table_admin_user;

    public function __construct($check_login = true) {
        parent::__construct($check_login);
        $this->table_admin_user = M('admin_user');
    }

    public function index() {
        //获取当前用户
        $userName = cookie('username');
        $this->assign('username', $userName);
        $this->display();
    }

    public function modifyPwd() {
        $uid = isWebLogin();
        if (!$uid) {
            fail_login('请重新登录');
        }
        if (!$_POST) {
            fail("需要post请求");
        }
        $data = get_auto_data($_REQUEST);
        $mpass = $data['mpass'];
        $newpass = $data['newpass'];
        $renewpass = $data['renewpass'];
        if (!$mpass) {
            fail_name("参数有误", 'mpass');
            return;
        }
        if (!$newpass) {
            fail_name("参数有误", 'newpass');
            return;
        }
        if (!$renewpass) {
            fail_name("参数有误", 'renewpass');
            return;
        }
        if ($newpass != $renewpass) {
            fail('两次密码不一致');
            return;
        }
        //判断原始密码是否正确
        // 验证会员是否存在
        $user = $this->table_admin_user->field('uid,username,salt,password,login_time')->where(array(
            'uid' => $uid
        ))->find();
        if (!$user) {
            fail('该用户尚未注册');
        }
        if (jiami($mpass . $user['salt']) != $user['password']) {
            fail('原始密码错误');
        }
        $yan = make_salt();
        $currentTime = time();
        $result = $this->table_admin_user->where(array(
            'uid' => $user['uid']
        ))->save(array(
            'password'    => jiami($newpass . $yan),
            'salt'        => $yan,
            'update_time' => $currentTime,
        ));
        if (!$result) {
            fail('修改密码失败，请重试');
        }
        succeed(null, '修改密码成功');
    }

    public function members() {
        $this->display();
    }
}